//Given a binary tree, return the level order traversal of its nodes' values. (i
//e, from left to right, level by level). 
//
// 
//For example: 
//Given binary tree [3,9,20,null,null,15,7], 
// 
//    3
//   / \
//  9  20
//    /  \
//   15   7
// 
// 
// 
//return its level order traversal as: 
// 
//[
//  [3],
//  [9,20],
//  [15,7]
//]
// 
// Related Topics 树 广度优先搜索


//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Example:
 * var ti = TreeNode(5)
 * var v = ti.`val`
 * Definition for a binary tree node.
 * class TreeNode(var `val`: Int) {
 *     var left: TreeNode? = null
 *     var right: TreeNode? = null
 * }
 */
class Solution {
    fun levelOrder(root: TreeNode?): List<List<Int>> {
        val result = ArrayList<List<Int>>()
        if (root == null) {
            return result
        }
        var nodes = java.util.LinkedList<TreeNode>()
        nodes.add(root)
        while (!nodes.isEmpty()) {
            val levelNode = LinkedList<Int>()
            val newNodes = LinkedList<TreeNode>()
            for (node in nodes) {
                levelNode.add(node.`val`)
                node.left?.let{newNodes.add(it)}
                node.right?.let{newNodes.add(it)}
            }
            result.add(levelNode)
            nodes = newNodes
        }
        return result
    }
}
//leetcode submit region end(Prohibit modification and deletion)
